﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;
using Lead2012.Utility;
using System.Web.Security;

namespace Lead2012.Controllers
{
    public class MySettingsController : BaseController
    {
        EFRoleProvider provider = new EFRoleProvider();
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        // Create an instance of repository
        UserManagementRepository umRepository = new UserManagementRepository();

        [Authorize(Roles = "Administrator,Registered")]
        public ActionResult Index()
        {
            UserViewModel model = new UserViewModel();

            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "FirstName");
            int userId = new UserRepository().GetSingle(User.Identity.Name).ID;
            umRepository.GetModel(model, userId);
           
            return View(model);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(FormCollection form)
        {
            UserRepository repository = new UserRepository();
            User model = new User();
            model = repository.GetSingle(User.Identity.Name);

            model.FirstName = form["FirstName"].ToString();
            model.LastName = form["LastName"].ToString();
            model.Fullname = string.Format("{0} {1}", model.FirstName, model.LastName);
            model.Email = form["Email"].ToString();
            if (!string.IsNullOrEmpty(form["Password"].ToString()))
                model.Password = EncryptionData.Encryption(form["Password"].ToString());

            repository.Save(model);

            return RedirectToAction("Index", "MySettings");
        }
     }
}